package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtScoreAddBo;
import com.ruoyi.system.bo.SysPtScoreQueryBo;
import com.ruoyi.system.bo.SysPtScoreEditBo;
import com.ruoyi.system.domain.SysPtScore;
import com.ruoyi.system.mapper.SysPtScoreMapper;
import com.ruoyi.system.vo.SysPtScoreVo;
import com.ruoyi.system.service.ISysPtScoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 学生成绩卡Service业务层处理
 *
 * @author 莫修
 * @date 2021-11-28
 */
@Service
public class SysPtScoreServiceImpl extends ServiceImpl<SysPtScoreMapper, SysPtScore> implements ISysPtScoreService {

    @Override
    public SysPtScoreVo queryById(Long scoreId){
        return getVoById(scoreId, SysPtScoreVo.class);
    }

    @Override
    public TableDataInfo<SysPtScoreVo> queryPageList(SysPtScoreQueryBo bo) {
        PagePlus<SysPtScore, SysPtScoreVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtScoreVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtScoreVo> queryList(SysPtScoreQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtScoreVo.class);
    }

    private LambdaQueryWrapper<SysPtScore> buildQueryWrapper(SysPtScoreQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtScore> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStuId() != null, SysPtScore::getStuId, bo.getStuId());
        lqw.eq(bo.getTeacherId() != null, SysPtScore::getTeacherId, bo.getTeacherId());
        lqw.eq(bo.getExpId() != null, SysPtScore::getExpId, bo.getExpId());
        lqw.eq(bo.getPreview() != null, SysPtScore::getPreview, bo.getPreview());
        lqw.eq(bo.getOperate() != null, SysPtScore::getOperate, bo.getOperate());
        lqw.eq(bo.getDiscipline() != null, SysPtScore::getDiscipline, bo.getDiscipline());
        lqw.eq(bo.getExp() != null, SysPtScore::getExp, bo.getExp());
        lqw.eq(bo.getFinalScore() != null, SysPtScore::getFinalScore, bo.getFinalScore());
        lqw.eq(bo.getIsEnable() != null, SysPtScore::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtScoreAddBo bo) {
        SysPtScore add = BeanUtil.toBean(bo, SysPtScore.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtScoreEditBo bo) {
        SysPtScore update = BeanUtil.toBean(bo, SysPtScore.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtScore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
